package com.aptemo.webetek.client.widgets.games;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EEditMode;
import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.widgets.DateTimeBox;
import com.aptemo.webetek.client.widgets.FocusTextArea;
import com.aptemo.webetek.client.widgets.FocusTextBox;
import com.aptemo.webetek.client.widgets.dialogs.CreateLabelDialog;
import com.aptemo.webetek.client.widgets.dialogs.DecisionDialog;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.LabelBean;
import com.aptemo.webetek.shared.PickBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class GameResume
		extends Composite implements GameResumePresenter.Display
{

	private static GameResumeUiBinder	uiBinder	= GWT.create(GameResumeUiBinder.class);

	interface GameResumeUiBinder extends UiBinder<Widget, GameResume>
	{
	}

	@UiField
	Label						titleField;

	@UiField
	Label						infoField;

	@UiField
	FocusTextBox				nameField;

	@UiField
	FocusTextArea				descField;

	@UiField
	FocusTextArea				picksPanel;

	// @UiField
	// ListBox betTypeList;

	@UiField
	Button						deleteBtn;

	@UiField
	Button						saveBtn;

	@UiField
	ListBox						labelsList;

	@UiField
	VerticalPanel				datePanel;

	@UiField
	Anchor						resultsLink;

	private DateTimeBox			dateField			= new DateTimeBox();

	private GameResultsDialog	resultsEditor		= new GameResultsDialog();

	private CreateLabelDialog	labelDialog			= new CreateLabelDialog("Creating label...");

	private DecisionDialog		deleteConfirmation	= new DecisionDialog("Deleting game...");

	public GameResume()
	{
		initWidget(uiBinder.createAndBindUi(this));
		datePanel.add(dateField);

		// Initial picks data (for new)
		ArrayList<String> items = new ArrayList<String>();
		setPicksData(items);
	}

	@Override
	public HasValue<Date> getDateField()
	{
		return dateField;
	}

	@Override
	public HasText getDescField()
	{
		return descField;
	}

	@Override
	public HasText getTitleField()
	{
		return titleField;
	}

	@Override
	public HasClickHandlers getSaveBtn()
	{
		return saveBtn;
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public void setEditMode(EEditMode mode, boolean results)
	{
		switch (mode)
		{
			case EADD:
				infoField.setText(Webetek.getConstants().addingData());

				deleteBtn.setVisible(false);
				saveBtn.setVisible(true);
				resultsLink.setVisible(false);
				break;
			case EEDIT:
				infoField.setText(Webetek.getConstants().modifyingData());

				saveBtn.setVisible(true);
				deleteBtn.setVisible(true);
				saveBtn.setEnabled(true);
				deleteBtn.setEnabled(true);

				resultsLink.setVisible(results);
				break;
			case EREADONLY:
				// infoField.setText("Game data...");

				nameField.setReadOnly(true);
				descField.setReadOnly(true);

				dateField.setEnabled(false);

				// betTypeList.setEnabled(false);
				labelsList.setEnabled(false);

				saveBtn.setVisible(false);
				deleteBtn.setVisible(false);

				setReadOnlyPicks(true);

				break;
		}

	}

	private void setReadOnlyPicks(boolean b)
	{

		picksPanel.setReadOnly(b);
		// for (int i = 0; i < picksPanel.getWidgetCount(); i++)
		// {
		// FocusTextBox ftb = (FocusTextBox) picksPanel.getWidget(i);
		// ftb.setReadOnly(b);
		// }
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	// @Override
	// public HasChangeHandlers getBetTypeList()
	// {
	// return betTypeList;
	// }

	@Override
	public void setPicksData(List<String> picks)
	{
		// TODO add PICK editor widget
		picksPanel.setText("");
		String res = "";
		for (String item : picks)
		{
			res += item + ", ";
		}
		// remove last...
		if (picks.size() > 0)
		{
			res = res.substring(0, res.length() - 2);
		}
		picksPanel.setText(res);
	}

	@Override
	public HasText getPicksField()
	{
		return picksPanel;
		// List<String> list = parsePicks();
		// if (list.size() > 0)
		// {
		// return list.get(index);
		// }
		// if (index >= picksPanel.getWidgetCount())
		// {
		// return new HTML("");
		// }
		// FocusTextBox item = (FocusTextBox) picksPanel.getWidget(index);
		// return "";
	}

	@UiHandler("nameField")
	void handleNameKeyPress(KeyPressEvent event)
	{
		saveBtn.setEnabled(true);
	}

	@UiHandler("descField")
	void handleDescriptionKeyPress(KeyPressEvent event)
	{
		saveBtn.setEnabled(true);
	}

	@Override
	public void setLabels(ArrayList<LabelBean> data)
	{
		labelsList.clear();
		labelsList.addItem("None", "");
		labelsList.addItem("Create new label", "");
		for (LabelBean item : data)
		{
			labelsList.addItem(item.getTitle(), item.getId());
		}
	}

	@Override
	public void setResult(EventBean bet)
	{
		if (bet.getStatus() == EnumEventStatus.ACTIVE)
		{
			resultsLink.setVisible(false);
			return;
		}
		resultsEditor.setData(bet);
	}

	@Override
	public void setSelectedLabel(String id)
	{
		for (int i = 0; i < labelsList.getItemCount(); i++)
		{
			String itemid = labelsList.getValue(i);

			if (itemid.equals(id))
			{
				labelsList.setItemSelected(i, true);
			}
		}
	}

	@Override
	public HasDecisionHandlers getNewLabelConfirmation()
	{
		return labelDialog;
	}

	@UiHandler("labelsList")
	void handleChange(ChangeEvent event)
	{
		// update list...
		int i = labelsList.getSelectedIndex();
		String id = labelsList.getValue(i);
		if (id == null || id.isEmpty())
		{
			labelDialog.center();
		}
	}

	@Override
	public HasText getNewLabelTextBox()
	{
		return labelDialog.getLabelField();
	}

	@Override
	public void addLabel(LabelBean label)
	{
		labelsList.addItem(label.getTitle(), label.getId());
		setSelectedLabel(label.getId());
	}

	@Override
	public String getSelectedLabel()
	{
		int i = labelsList.getSelectedIndex();

		if (i < 0)
			return "";
		return labelsList.getValue(i);
	}

	@Override
	public HasText getNameField()
	{
		return nameField;

	}

	@Override
	public String getResultPickValue()
	{
		return resultsEditor.getSelectedPick().getId();
	}

	@Override
	public HasText getResultCommentField()
	{
		// return resultsComment;
		return resultsEditor.getCommentField();
	}

	@UiHandler("nameField")
	void nameValueChangeHandler(ValueChangeEvent<String> event)
	{
		titleField.setText(event.getValue());
	}

	@UiHandler("nameField")
	void nameKeyPressHandler(KeyPressEvent event)
	{
		// String text = titleField.getText(). event.getCharCode();
		titleField.setText(nameField.getText());
	}

	@Override
	public String getSelectedResult()
	{
		PickBean pick = resultsEditor.getSelectedPick();

		return pick.getId();
	}

	@UiHandler("resultsLink")
	void resultsEditorClickHandler(ClickEvent event)
	{
		resultsEditor.center();
	}

	@Override
	public HasDecisionHandlers getResultsConfirmation()
	{
		return resultsEditor;
	}

	// @Override
	// public EnumEventType getSelectedBetType()
	// {
	// int i = betTypeList.getSelectedIndex();
	//
	// String s = betTypeList.getValue(i);
	// if (s.isEmpty())
	// {
	// return null;
	// }
	//
	// return EnumEventType.valueOf(s);
	// }

	// @Override
	// public void setBetType(EnumEventType type)
	// {
	// for (int i = 0; i < betTypeList.getItemCount(); i++)
	// {
	// String val = betTypeList.getValue(i);
	//
	// if (EnumEventType.valueOf(val) != null &&
	// EnumEventType.valueOf(val).equals(type))
	// {
	// betTypeList.setSelectedIndex(i);
	// break;
	// }
	// }
	//
	// }

	@UiHandler("deleteBtn")
	void deleteHandler(ClickEvent event)
	{
		deleteConfirmation.setQuestionString("Are you sure you want to delete " + nameField.getText() + "?");
		deleteConfirmation.center();
	}

	@Override
	public HasDecisionHandlers getDeleteConfirmation()
	{
		return deleteConfirmation;
	}

	@Override
	public void hideDeleteConfirmationDialog()
	{
		deleteConfirmation.hide();
	}

	@Override
	public void hideNewLabelConfirmationDialog()
	{
		labelDialog.hide();
	}

	@Override
	public void hideResultsConfirmationDialog()
	{
		resultsEditor.hide();
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
